package com.cwd.arm.inter.conf.AuthHandler;

import com.cwd.arm.common.dto.session.UserDto;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.user.UserVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by yan on 21-11-19.
 */
public class SuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        UserDto userDto = (UserDto)authentication.getPrincipal();
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(new ObjectMapper().writeValueAsString(ResponseVo.<UserVo>BUILDER().success(DtoToVoUtil.convertToUserVo(userDto))));
        out.flush();
        out.close();
    }

}
